﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.admin.basic
{
    public partial class banner : BaseAdmin
    {
        tbBanner oBanner;
     


        private void bind()
        {

            if ((base.Request.QueryString["flashid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    BaseFactory.theBanner.Delete(int.Parse(base.Request.QueryString["flashid"].ToString()));
                }
                base.Response.Write("<script language=javascript>alert('删除成功');window.location.href='banner.aspx';</script>");
                base.Response.End();
            }
            DataSet set2 = BaseFactory.theBanner.GetList(string.Format("position='{0}'", this.ddlPosition.SelectedItem.Value));
                this.DataList2.DataSource = set2;
                this.DataList2.DataBind();
          
        }

        public void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
            HttpFileCollection files = HttpContext.Current.Request.Files;
            StringBuilder builder = new StringBuilder("");
            string[] strArray = base.Request.Form["text"].Split(new char[] { ',' });
            for (int i = 0; i < files.Count; i++)
            {
                if (files[i].FileName.Length > 0)
                {
                    HttpPostedFile file = files[i];
                    if ((file.ContentLength / 1024) > 1024)
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---不能大于1024k<br>");
                        break;
                    }
                    string extension = Path.GetExtension(file.FileName);
                    if (((extension != ".jpg") && (extension != ".JPG")) && ((extension != ".gif") && (extension != ".GIF")))
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---图片格式不对，只能是jpg或gif<br>");
                        break;
                    }
                }
            }
            if (builder.Length <= 0)
            {
                string str3 = "pic001";
                string path = base.Server.MapPath("../uploadfile/flash") + @"\" + str3;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Random random = new Random();
                int num2 = 1;
             
                for (int j = 0; j < files.Count; j++)
                {
                    HttpPostedFile file2 = files[j];
                    string fileName = "";
                    string str6 = "";
                    string imgpath = "";
                    fileName = Path.GetFileName(file2.FileName);
                    string str8 = random.Next(100, 0x5f5e100).ToString() + num2.ToString();
                    string str9 = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + str8;
                    if (fileName.Length > 0)
                    {
                        str6 = Path.GetExtension(file2.FileName);
                        string filename = path + @"\" + str9 + str6;
                        file2.SaveAs(filename);
                        imgpath = @"flash\" + str3 + @"\" + str9 + str6;
                    }
                    if (str6.ToString() != "")
                    {
                        oBanner = new tbBanner();
                        oBanner.companyid = int.Parse(base.Administrator.CompanyId);
                        oBanner.flashpic = imgpath;
                        oBanner.flashurl = strArray[j];
                        oBanner.position = this.ddlPosition.SelectedItem.Value;
                        BaseFactory.theBanner.Add(oBanner);
                    }
                    num2++;
                }
                base.Response.Write("<script language='javascript'>alert('广告更新成功');window.location.href='banner.aspx';</script>");
                base.Response.End();
            }
        }

        public void Page_Load(object sender, EventArgs e)
        {
          
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AdvertisingSet);
                //this.BindClass();
              this.bind();
              
            }
        }





        //private void BindClass()
        //{
        //    string sql = "select classId,ClassName from DB_Class ";
        //    DataTable table = new DataTable();
        //    table = SQLAccess.GreatDs(sql).Tables[0];
        //    this.ddlClass.DataTextField = "className";
        //    this.ddlClass.DataValueField = "classId";
        //    this.ddlClass.DataSource = table;
        //    this.ddlClass.DataBind();
        //}



        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlPosition.SelectedItem.Value == "首页默认 ")
            {
                this.lblDescription.Text = "图片尺寸：780*280";
            }
            else
            {
                this.lblDescription.Text = "图片尺寸：1000*60";
            }

                 this.bind();
        }


    }
}